import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { RouterProvider } from 'react-router-dom'
import './index.css'
import router from './router'
import axios from 'axios'
import { ConfigProvider } from 'antd'
import zhCN from 'antd/lib/locale/zh_CN'

// 配置axios默认设置
axios.defaults.timeout = 10000; // 请求超时时间
axios.defaults.withCredentials = false; // 默认不发送凭证
// 如果API地址是固定的，可以设置baseURL
axios.defaults.baseURL = 'http://localhost:5118';
axios.defaults.headers.common['Content-Type'] = 'application/json'; // 设置默认Content-Type

// 添加请求拦截器
axios.interceptors.request.use(
  config => {
    // 对于DELETE请求，移除Content-Type头
    if (config.method?.toLowerCase() === 'delete') {
      // 删除Content-Type头，让浏览器自动设置
      delete config.headers['Content-Type'];
    }
    
    // 确保API请求不发送凭证
    if (config.url?.includes('/api/')) {
      config.withCredentials = false;
    }
    
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 添加响应拦截器
axios.interceptors.response.use(
  response => {
    // 直接返回响应对象
    return response;
  },
  error => {
    console.error('请求错误', error);
    return Promise.reject(error);
  }
);

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <ConfigProvider locale={zhCN}>
      <RouterProvider router={router} />
    </ConfigProvider>
  </StrictMode>,
)
